<template>
  <div class="header">
    <a href="/">
      <img class="logo" src="@/assets/logo.png" width="20px" />
      <span class="company">梦学谷会员管理系统</span>
    </a>
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        {{this.user.name}}
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="a" icon="el-icon-edit">修改密码</el-dropdown-item>
        <el-dropdown-item command="b" icon="el-icon-s-unfold">退出系统</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <!-- 修改密码对话框 -->
    <el-dialog title="商品编辑" :visible.sync="dialogFormVisible" width="400px">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="width: 300px" class="demo-ruleForm">
        <el-form-item label="密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="$refs['ruleForm'].resetFields()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {logout} from "@/api/login"
import passwordApi from "@/api/password"

export default {
  data(){
    // 自定义密码校验
    const validateOldPass = (rule, value, callback) => {
      passwordApi.checkPwd(this.user.id, value).then(response => {
        const resp = response.data
        if(resp.flag){
          callback()
        }else{
          callback(new Error(resp.message))
        }
      })
    }

    // 自定义确认密码校验
    const validatePass = (rule, value, callback) => {
      if(value == this.ruleForm.pass){
        callback()
      }else{
        callback(new Error("新密码与确认密码不一致"))
      }
    }

    return{
      user: this.$store.state.user.user,
      ruleForm: {
        oldPass: '',
        pass: '',
        checkPass: ''
      },
      dialogFormVisible: false,
      rules: {
        oldPass: [
          {required: true, message: '原密码不能为空', trigger: 'blur'},
          {validator: validateOldPass, trigger: 'blur'}
        ],
        pass: [
          {required: true, message: '新密码不能为空', trigger: 'blur'}
        ],
        checkPass: [
          {required: true, message: '确认密码不能为空', trigger: 'blur'},
          {validator: validatePass, trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    handleCommand(command) {
      // this.$message("点击了" + command);
      switch (command) {
        // 点击了修改密码选项
        case 'a':
          this.handlePwd()
          break;
          // 点击了退出系统选项
        case 'b':
          this.handleLogout()
          break;
      
        default:
          break;
      }
    },
    // 退出系统
    handleLogout(){
      this.$store.dispatch('Logout').then(response => {
        if(response.flag){
          this.$router.push('/login')
        }else{
          this.message({
            message: response.message,
            type: 'warning'
          })
        }
      }).catch(error => {

      })
    },
    // 修改密码
    handlePwd(){
      this.dialogFormVisible = true
      this.$nextTick(() => {
            this.$refs['ruleForm'].resetFields()
      })
    },
    // 提交修改密码
    submitForm(formName){
      this.$refs[formName].validate(valid => {
        if(valid){
          console.log('密码修改成功')
          passwordApi.updatePwd(this.user.id, this.ruleForm.checkPass).then(response => {
            const resp = response.data
            this.$message({
              message: resp.message,
              type: resp.flag ? 'success' : 'error'
            })
            if(resp.flag){
              this.handleLogout()
              this.dialogFormVisible = false
            }
          })
        }else{
          return false
        }
      })
    }
  }
};
</script>

<style scoped>
.logo {
  vertical-align: middle;
  padding: 0 10px 0 40px;
}
.company {
  position: absolute;
  color: #fff;
}
.el-dropdown {
  float: right;
  margin-right: 40px;
  cursor: pointer;
  color: #fff;
}
</style>